package com.ifar.audiosdk.api

import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.DeviceUtils
import com.ifar.audiosdk.base.IfarBaseRepository
import com.ifar.audiosdk.common.IfarBaseResponse
import com.ifar.audiosdk.common.IfarConfig
import com.ifar.audiosdk.model.login.model.LoginEntity
import com.ifar.audiosdk.model.main.model.CheckUserCallStateEntity
import com.ifar.audiosdk.model.main.model.UpdateUsersCallStateEntity
import com.ifar.audiosdk.model.main.model.UserListEntity
import java.util.*

class ApiRepository : IfarBaseRepository() {

    /**
     * 登录
     */
    suspend fun login(
        username: String,
        password: String,
    ): IfarBaseResponse<LoginEntity> {
        val peerId = (Random().nextInt(1000000) + 1000000).toString() + ""
        IfarConfig.peer_id = peerId
        val signId = DeviceUtils.getUniqueDeviceId()
        IfarConfig.sign_id = signId

        return apiCall {
            IfarRetrofitClient.service.login(
                "loginIn",
                username,
                password,
                signId,
                peerId,
                IfarConfig.USER_GROUP,
                IfarConfig.REQUEST_FROM,
                AppUtils.getAppVersionName()
            )
        }
    }

    /**
     * 获取用户列表
     */
    suspend fun getUserList(): IfarBaseResponse<UserListEntity> {
        return apiCall {
            IfarRetrofitClient.service.getUserList(
                "getUserList",
                IfarConfig.company_id,
                IfarConfig.id,
                IfarConfig.token
            )
        }
    }

    /**
     * 更新用户通话状态，可多个
     */
    suspend fun updateUsersCallState(
        userIDs: String, channel_id: String
    ): IfarBaseResponse<UpdateUsersCallStateEntity> {
        return apiCall {
            IfarRetrofitClient.service.updateUsersCallState(
                "updateUsersCallState",
                userIDs,
                IfarConfig.id,
                IfarConfig.token,
                channel_id
            )
        }
    }

    /**
     * 检查用户通话状态，可多个
     */
    suspend fun checkUserCallState(userIDs: String): IfarBaseResponse<CheckUserCallStateEntity> {
        return apiCall {
            IfarRetrofitClient.service.checkUserCallState(
                "checkUserCallState",
                userIDs,
                IfarConfig.id,
                IfarConfig.token
            )
        }
    }

}